import {Component} from '@angular/core';
import { NavController } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
import { Platform } from 'ionic-angular';

declare var BMap, BDLocation;
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {
  map: any;
  constructor(private platform: Platform,public navCtrl: NavController,private geolocation: Geolocation) {
    // this.ionViewDidLoad();
  }

  ionViewWillEnter2() {
    this.map = new BMap.Map("Bmap");
    this.map.centerAndZoom(new BMap.Point(116.331398,39.897445), 11);
    this.map.addControl(new BMap.NavigationControl());
    this.map.enableScrollWheelZoom(true);
    this.loadMap();
  }
  loadMap(){
    this.platform.ready().then(()=>{

    this.geolocation.getCurrentPosition().then((data) => {
      // resp.coords.latitude
      // resp.coords.longitude
      this.map.clearOverlays();
      var new_point = new BMap.Point(data.coords.longitude,data.coords.latitude);
      var marker = new BMap.Marker(new_point);  // 创建标注
      this.map.addOverlay(marker);              // 将标注添加到地图中
      this.map.panTo(new_point);
    }).catch((error) => {
      alert('Error getting location'+error);
    });
    });
    // let watch = this.geolocation.watchPosition();
    // watch.subscribe((data) => {
    //   // data can be a set of coordinates, or an error (if an error occurred).
    //   // data.coords.latitude
    //   // data.coords.longitude
    //   alert("alert");
    //   alert(data.coords.latitude+"=="+data.coords.longitude);
    //   this.map.clearOverlays();
    //   var new_point = new BMap.Point(data.coords.latitude,data.coords.longitude);
    //   var marker = new BMap.Marker(new_point);  // 创建标注
    //   this.map.addOverlay(marker);              // 将标注添加到地图中
    //   this.map.panTo(new_point);
    // });

  }
}
